import re
import datetime

from Config import *
from Parser import *
from InputProperty import InputProperty
from OutputProperty import OutputProperty

# Ed
StrEd = "苏源"

# ZhiFuBao
StrZFB = "支付宝"

# CaiFuTong
StrCFT = "财付通"

# Annual Fee
AnnualFee = "年费 300.00元"
AnnualTimes = "刷卡 6次免年费 300.00元"

class ParserZS(Parser):
    def __init__(self, owner):
        super().__init__(owner)
        self.__billDate = None

    def _doParse(self, line):
        if self.__handleBillDate(line):
            return
        if self.__isUselessLine(line):
            return        
        input = InputProperty()
        input.Datetime = self.__getItemDate(line)
        input.Amount = self.__getItemAmount(line)
        input.Description = self.__getItemDescription(line)
        self.InputProperties.append(input)

    def _preSingleParse(self):
        super()._preSingleParse()
        self.__billDate = None

    def _generateOutputs(self):
        for input in self.InputProperties:
            payMethod = self.__getItemPayMethod(input)
            if payMethod == PayMethod.Alipay or payMethod == PayMethod.Wechat:
                continue
            output = OutputProperty()
            output.Owner = self.BillOwner
            output.Datetime = input.Datetime
            output.Amount = input.Amount
            output.Currency = self.__getItemCurrency()
            output.Category, output.Auto = self.__getItemCategory(input)
            output.PayMethod = payMethod
            output.Note = self.__getItemNote(input)
            self.OutputProperties.append(output)
        
    def __handleBillDate(self, line):
        # Year Month Day
        match = re.match("(\d{4})年(\d{2})月(\d{2})日", line)
        if  match and not self.__billDate:
            year = int(match.group(1))
            month = int(match.group(2))
            day = int(match.group(3))
            self.__billDate = datetime.date(year, month, day)
            return True
        return False

    def __isUselessLine(self, line):
        return not re.search("\d{2}\/\d{2} \d{2}\/\d{2}", line) or re.search(AnnualFee, line) or re.search(AnnualTimes, line)
    
    def __getItemDate(self, line):
        search = re.search("(\d{2})\/(\d{2})", line)
        month = int(search.group(1))
        day = int(search.group(2))
        year = self.__billDate.year if month == self.__billDate.month or self.__billDate.month > 1 else self.__billDate.year - 1
        return datetime.date(year, month, day)
    
    def __getItemAmount(self, line):
        return float(line.split()[3].replace(",", ""))

    def __getItemDescription(self, line):
        return line.split()[2]
        
    def __getItemCurrency(self):
        return Currency.CNY
        
    def __getItemCategory(self, input):
        note = self.__getItemNote(input)
        return (self._stringToCategory(self.CategoryPairs[note]), True) if note in self.CategoryPairs else (Category.Other, False)

    def __getItemPayMethod(self, input):
        if re.search(StrZFB, input.Description):
            return PayMethod.Alipay
        if re.search(StrCFT, input.Description):
            return PayMethod.Wechat
        return PayMethod.Credit

    def __getItemNote(self, input):
        description = re.sub(StrZFB, "", input.Description)
        description = re.sub(StrCFT, "", description)
        return re.sub("-", "", description).strip()
